package com.hex.ds.hdtp.core.web.script.transfer.controller;

import com.hex.bigdata.hbda.annotation.Log;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.script.transfer.dto.request.ScriptConvertTargetListRequestDto;
import com.hex.ds.hdtp.core.app.script.transfer.service.IScriptConvertTargetListService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.script.transfer.converter.IScriptConvertTargetListVoConverter;
import com.hex.ds.hdtp.core.web.script.transfer.vo.ScriptConvertTargetListVo;
import com.hex.ds.hdtp.core.web.script.transfer.vo.query.ScriptConvertTargetListQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

@Slf4j
@RestController
@Api(tags = "03-脚本迁移：脚本转换后目标脚本清单")
@RequestMapping("/api/script/convert/target/list")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ScriptConvertTargetListController {

    private final IScriptConvertTargetListService scriptConvertTargetListService;

    private final IScriptConvertTargetListVoConverter converter;

    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [scriptConvertTargetListQueryVo, pageVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/16 17:50 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping
    @ApiOperation("脚本转换验证清单-分页查询")
    public ResponseEntity queryListByPage(ScriptConvertTargetListQueryVo scriptConvertTargetListQueryVo, PageVo pageVo) {
        log.debug("进入到【ScriptConvertTargetListController.queryListByPage】方法,参数：{}" + scriptConvertTargetListQueryVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        ScriptConvertTargetListRequestDto requestDto = converter.toDTOForQueryVo(scriptConvertTargetListQueryVo);
        return ResponseEntity.ok(scriptConvertTargetListService.queryListByPage(requestDto, pageDto));
    }

    /*
     * @Method querySuccessListByPage <br>
     * @param scriptConvertSourceListQueryVo
     * @param pageVo <br>
     * @Return org.springframework.http.ResponseEntity <br>
     * @Description 脚本转换清单-分页查询最新成功的脚本<br>
     * @Author Yin.Yang<br>
     * @Date 2023/8/30 10:22 <br>
     * @Version V1.0<br>
     */
    @GetMapping("/latestSuccess")
    @ApiOperation("脚本转换清单-分页查询最新成功的脚本")
    public ResponseEntity querySuccessListByPage(ScriptConvertTargetListQueryVo scriptConvertTargetListQueryVo, PageVo pageVo) {
        log.debug("进入到【ScriptConvertTargetListController.querySuccessListByPage】方法,参数：{}" + scriptConvertTargetListQueryVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        ScriptConvertTargetListRequestDto requestDto = converter.toDTOForQueryVo(scriptConvertTargetListQueryVo);
        return ResponseEntity.ok(scriptConvertTargetListService.querySuccessListByPage(requestDto, pageDto));
    }

    /*
     * @Method: queryById <br>
     * @Param: [pkId] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：获取转换后清单对象<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/15 18:46 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping(value = {"/{pkId}"})
    @ApiOperation("获取转换后清单对象")
    public ResponseEntity queryById(@PathVariable("pkId") final String pkId) {
        log.debug("进入【ScriptConvertTargetListController.queryById】方法,参数{}", pkId);
        return ResponseEntity.ok(scriptConvertTargetListService.queryById(pkId));
    }

    /*
     * @Method: getContrastFileContent <br>
     * @Param: [crunSourceListPkId, histSourceListPkId, fileName] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：获取指定的转换历史文件对比内容<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/16 16:46 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping(value = {"/{crunSourceListPkId}/{histSourceListPkId}"})
    @ApiOperation("获取指定的转换历史文件对比内容")
    public ResponseEntity getContrastFileContent(@PathVariable("crunSourceListPkId") final String crunSourceListPkId,
                                                 @PathVariable("histSourceListPkId") final String histSourceListPkId,
                                                 @RequestParam @NotBlank String convertFileName) {
        log.debug("进入【ScriptConvertTargetListController.getContrastFileContent】方法,参数");
        return ResponseEntity.ok(scriptConvertTargetListService.queryConvertHistoryCompareContent(crunSourceListPkId, histSourceListPkId, convertFileName));
    }

    /*
     * @Method: updateFileContent <br>
     * @Param: [scriptConvertSourceListVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：更新指定转换后文件内容<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/16 17:18 <br>
     * @Version： V2.0.2<br>
     */
    @Log("更新指定转换后文件内容")
    @PutMapping(value = {"/update/file/content"})
    @ApiOperation("更新指定转换后文件内容")
    public ResponseEntity updateFileContent(@RequestBody ScriptConvertTargetListVo scriptConvertSourceListVo) {
        log.info("进入【ScriptConvertTargetListController.updateFileContent】方法,参数：", scriptConvertSourceListVo.toString());
        scriptConvertTargetListService.updateFileContent(converter.toDTO(scriptConvertSourceListVo));
        return ResponseEntity.ok("更新成功");
    }

    @Log("重新验证脚本")
    @PutMapping(value = {"/reverify/{pkId}"})
    @ApiOperation("重新验证脚本")
    public ResponseEntity reverify(@PathVariable("pkId") final String pkId) {
        log.info("进入【ScriptConvertTargetListController.reverify】方法,参数：", pkId);
        scriptConvertTargetListService.reverify(pkId);
        return ResponseEntity.ok("操作成功");
    }
}
